package com.base.cn.platform.os.service.op.keyword;

import com.base.cn.platform.os.common.j2cache.CustomCacheChannel;
import com.base.cn.platform.os.common.j2cache.J2CacheRegion;
import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.dao.op.keyword.OpKeywordDao;
import com.base.cn.platform.os.entity.op.keyword.OpKeyword;
import com.base.cn.platform.os.service.manage.user.SysUserBiz;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.stream.IntStream;

/**
 * 敏感词Biz
 * @author zsw
 * @create 2018/4/19
 */
@Service
public class OpKeywordBiz extends BaseBiz<OpKeyword, OpKeywordDao> {
    private static final String KEY_WORD_DATA_CACHE_KEY="key_word_data_cache_key";
    @Autowired
    private SysUserBiz sysUserBiz;
    @Autowired
    private CustomCacheChannel customCacheChannel;

    /**
     * 获取所有的敏感词
     * @return 数据
     */
    public String findAllKeyword(){
        StringBuffer stringBuffer = (StringBuffer)customCacheChannel.get(J2CacheRegion.THIRTY_DAY_REGION.getKey(),KEY_WORD_DATA_CACHE_KEY).getValue();
        if(ObjectUtils.isEmpty(stringBuffer)){
            StringBuffer _stringBuffer = new StringBuffer();
            List<OpKeyword> keywordList = this.find(" 1=1",null,null);
            if(ObjectUtils.isNotEmpty(keywordList)){
                IntStream.range(0,keywordList.size()).forEach(index->{
                    String word = keywordList.get(index).getWord();
                    if(index < keywordList.size() -1){
                        _stringBuffer.append(word).append("|");
                    }else{
                        _stringBuffer.append(word);
                    }
                });
            }
            if(_stringBuffer.length() >0){
                stringBuffer = _stringBuffer;
                customCacheChannel.set(J2CacheRegion.THIRTY_DAY_REGION.getKey(),KEY_WORD_DATA_CACHE_KEY,stringBuffer);
            }
        }
        if(stringBuffer!=null && stringBuffer.length() >0){
            return stringBuffer.toString();
        }else{
            return null;
        }
    }

    /**
     * 查询敏感词数量
     * @param keyword 敏感词实体
     */
    public BigDecimal count(OpKeyword keyword){
        return this.count(this.getWhereSql(keyword));
    }

    /**
     * 保存敏感词
     * @param keyword 敏感词实体
     */
    public void saveKeyword(OpKeyword keyword){
        this.save(keyword);
        customCacheChannel.evict(J2CacheRegion.THIRTY_DAY_REGION.getKey(),KEY_WORD_DATA_CACHE_KEY);
    }

    /**
     * 查询敏感词列表
     * @param page 页数
     * @param keyword 敏感词实体
     * @return
     */
    public PageInfo<OpKeyword> queryKeywordList(Pagination page, OpKeyword keyword,boolean sysUserData){
        String whereSql = this.getWhereSql(keyword);
        PageInfo<OpKeyword> keywordPageInfo = this.findPage(whereSql, page, null);
        keywordPageInfo.setList(this.setKeywordDate(keywordPageInfo.getList(),sysUserData));
        return keywordPageInfo;
    }

    /**
     * 设置扩展数据
     * @param keywordList 敏感词列表
     * @param sysUserData 是否设置创建人数据
     * @return
     */
    public List<OpKeyword> setKeywordDate(List<OpKeyword> keywordList,boolean sysUserData){
        if (ObjectUtils.isNotEmpty(keywordList)){
            if (sysUserData){
                sysUserBiz.setSysUserData(keywordList,sysUserData);
            }
        }
        return keywordList;
    }

    /**
     * 删除敏感词
     * @param keywordIds
     */
    public void deleteKeyword(String keywordIds) {
        this.deleteWhereSql(" id in ("+ keywordIds +") ");
        customCacheChannel.evict(J2CacheRegion.THIRTY_DAY_REGION.getKey(),KEY_WORD_DATA_CACHE_KEY);
    }

    /**
     * sql模板
     * @param keyword 查询条件
     * @return
     */
    private String getWhereSql(OpKeyword keyword){
        String whereSql = " 1=1 ";
        if (StringUtils.isNotEmpty(keyword.getWord())) {
            whereSql += " and word like concat('%','" + keyword.getWord() + "','%')";
        }
        if (DataUtil.idIsNotNull(keyword.getCreateUserId())) {
            whereSql += " and createUserId =" + keyword.getCreateUserId();
        }
        if (StringUtils.isNotEmpty(keyword.getCreateTime())) {
            whereSql += " and createTime >= '" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(keyword.getCreateTime()) + "'";
        }
        if (DataUtil.idIsNotNull(keyword.getId())){
            whereSql += " and id in ("+ keyword.getId() +")";
        }
        return whereSql;
    }
}
